package com.xc.freeapp.account.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.alibaba.fastjson.JSONArray;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.google.gson.JsonArray;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Timingvisit;
import com.xc.freeapp.entity.Vaccineperson;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.VaccinepersonService;
import com.xc.freeapp.service.VaccinesService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.JsonUtil;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;
@Controller
@RequestMapping("/v2/VaccinePerson")
@Api(value="v2/vaccinePerson",description = "接种人")
public class VaccinePersonControllerV2 extends AbstractController {
	@Autowired
	private VaccinepersonService vaccinepersonServiceImpl;
	@Autowired
	private VaccinesService vaccinesServiceImpl;
	@TokenAnnotation(required=true)
	@RequestMapping(value = "{id}",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取接种人信息")
	public Vaccineperson selectByPrimaryKey(@PathVariable("id") Integer id) throws  BaseException{
		Vaccineperson vaccineperson=null;
		vaccineperson= vaccinepersonServiceImpl.selectByPrimaryKey(id);
		if(vaccineperson.getHeadpic()!=null&&!"".equals(vaccineperson.getHeadpic())){
			vaccineperson.setHeadpic(UploadUtil.pathstoList(vaccineperson.getHeadpic()).get(0));
			
		}
		vaccineperson.getRemindtime1().setYear(70);
		vaccineperson.getRemindtime2().setYear(70);
		return vaccineperson;
	}
	/**
	 * 保存接种人信息
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存接种人信息")
	public ResponseBean saveVaccinePerson(
			@RequestBody @Valid Vaccineperson vaccineperson)
			throws BaseException {
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
		
			String headpic = UploadUtil.uploads(JSONArray.parseArray(vaccineperson.getHeadpic(), String.class),authInfo.getAppIntId());
			vaccineperson.setHeadpic(headpic);
			vaccineperson.setUserid(Integer.valueOf(authInfo.getUserId()));
			vaccineperson.setDelflag("t");
			vaccineperson.getRemindtime1().setYear(71);
			vaccineperson.getRemindtime2().setYear(71);
			vaccinepersonServiceImpl.addVaccineperson(vaccineperson);
			log.info("新添加接种人id"+vaccineperson.getId());
			if( vaccineperson.getVaccines().size()>0){
				for (String vaccineid : vaccineperson.getVaccines()) {
					vaccinesServiceImpl.updateifdo(Integer.valueOf(vaccineperson.getId()),
							Integer.valueOf(vaccineid),1 );
				}
			}
			return ResponseBean.getSuccess();
		} catch (BaseException e) {
			throw e;
		} catch (Exception e) {
			log.error(authInfo.getUserId() + "保存接种人信息失败" + e.getMessage());
			e.printStackTrace();
			throw new BaseException(Constant.GLOBAL_ERROR);
		}

	}
	@TokenAnnotation(required=true)
	@RequestMapping(value = "",method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "更新接种人信息")
	public Vaccineperson updateVaccinePerson(
			@RequestBody Vaccineperson vaccineperson)
			throws BaseException {
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
		  if(vaccineperson.getHeadpic()!=null&&!"".equals(vaccineperson.getHeadpic())){
			  String headpic = UploadUtil.upload(vaccineperson.getHeadpic(),authInfo.getAppIntId());
				List list =new ArrayList();
				list.add(headpic);
				vaccineperson.setHeadpic(JSONArray.toJSONString(list));
		  }
			vaccineperson.setUserid(Integer.valueOf(authInfo.getUserId()));
			vaccineperson.getRemindtime1().setYear(71);
			vaccineperson.getRemindtime2().setYear(71);
			vaccinepersonServiceImpl.updateVaccineperson(vaccineperson);
			log.debug(vaccineperson.getHeadpic());
			if(vaccineperson.getHeadpic()!=null&&!"".equals(vaccineperson.getHeadpic())){
				vaccineperson.setHeadpic(UploadUtil.pathstoList(vaccineperson.getHeadpic()).get(0));
				
			}
			vaccineperson.getRemindtime1().setYear(70);
			vaccineperson.getRemindtime2().setYear(70);
			return vaccineperson;
		} catch (BaseException e) {
			throw e;
		} catch (Exception e) {
			log.error(authInfo.getUserId() + "更新接种人信息失败" + e.getMessage());
			e.printStackTrace();
			throw new BaseException(Constant.GLOBAL_ERROR);
		}

	}
	/**
	 * 获取接种人信息
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取接种人信息")
	public ResponsePage<List<Vaccineperson>>  getVaccinePersonList(
			@RequestParam(value = "page", required = false, defaultValue="0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="50") Integer limit)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			Map<String, String> map = new HashMap<String, String>();
			PageBounds pageBounds = new PageBounds(page, limit);
			map.put("userId", authInfo.getUserId());
			List<Vaccineperson> vaccinepersons = vaccinepersonServiceImpl
					.getVaccineperson(map,pageBounds);
			Paginator paginator =((PageList)vaccinepersons).getPaginator();
			for (Vaccineperson vaccineperson : vaccinepersons) {
				if(vaccineperson.getHeadpic()!=null&&!"".equals(vaccineperson.getHeadpic())){
					vaccineperson.setHeadpic(UploadUtil.pathstoList(vaccineperson.getHeadpic()).get(0));
					
				}
				vaccineperson.getRemindtime1().setYear(70);
				vaccineperson.getRemindtime2().setYear(70);
			}
			
			
			return new ResponsePage<List<Vaccineperson>>(vaccinepersons, paginator);
		} catch (Exception e) {
			e.getStackTrace();
			log.error(authInfo.getUserId() + "获取接种人信息失败");
			throw new BaseException(Constant.GLOBAL_ERROR);
		}

	}
//	@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
//	@TokenAnnotation(required=true)
//	@ResponseBody
//	@ApiOperation(value = "删除接种人信息")
	public ResponseBean delVaccinePerson(@PathVariable("id") Integer id)
			throws BaseException {
			vaccinepersonServiceImpl.delVaccineperson(id);
			return ResponseBean.getSuccess();
	}
	

}
